package org.devocean.groupbuying.service.impl;

import java.util.List;

import org.devocean.groupbuying.dao.CustomerDAO;
import org.devocean.groupbuying.domain.Customer;
import org.devocean.groupbuying.service.CustomerService;
import org.devocean.groupbuying.utils.Hashing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CustomerServiceImpl implements CustomerService {

	@Autowired
	private CustomerDAO customerDAO;


	@Transactional(readOnly = true)
	public Customer getCustomerById(Integer id) {

		return customerDAO.findCustomerById(id);
	}

	@Transactional
	public Customer addOrUpdateCustomer(Customer customer) {
		if (customer.getCustomerID() == null) {
			try {
				String psswrd = Hashing.getHashCode(customer.getPassword());
				customer.setPassword(psswrd);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return customerDAO.saveCustomer(customer);
		} else {
			return customerDAO.updateCustomer(customer);
		}

	}

	@Transactional
	public void removeCustomer(Integer id) {
		customerDAO.deleteCustomer(id);
	}

	public long countCustomers() {

		return customerDAO.countCustomers();
	}

	@Transactional
	public List<Customer> getAllCustomers() {
		return customerDAO.findAllCustomers();
	}

	@Transactional
	public Customer updatePasswordCustomer(Customer customer) {

		try {
			String psswrd = Hashing.getHashCode(customer.getPassword());
			customer.setPassword(psswrd);
			customerDAO.updateCustomer(customer);
		} catch (Exception e) {
			e.getMessage();
		}

		return customer;
	}

	public boolean verifyCustomer(Customer customer) {
		
			if (customerDAO.countCustomerByEmail(customer.getEmail()) >= 1)
				return true;
			
		
		
			return false;
	}
	

	public Customer getCustomerByUuid(String customerUuid) {
		return customerDAO.findCustomerByUuid(customerUuid);
	}

	public Customer getCustomerByEmail(String email) {
		return customerDAO.findCustomerByEmail(email);
	}

}
